package com.yanyilu.assess.controller;

import com.yanyilu.assess.common.R;
import com.yanyilu.assess.entity.Position;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("position")
public class positionController {

    @Value("${uploadDir}")
    private String uploadDir;

    /**
     *
     * @param file
     * @return
     */
    @RequestMapping("/import")
    public HashMap<String, Object> upload(@RequestParam("file") MultipartFile file) {

        HashMap<String, Object> result = new HashMap<String, Object>();
        if (file.isEmpty()) {
            result.put("status", "文件上传失败");
            result.put("code", "500");
            result.put("msg", "文件大小为0");
            return result;
        } else {
            try {
                File dic = new File(uploadDir);
                if (dic.exists() && dic.isDirectory()) {
                    byte[] bytes = file.getBytes();
                    Path path = Paths.get(uploadDir + file.getOriginalFilename());
                    Files.write(path, bytes);

                    R r = new R();
                    r.ok(90001,"文件上传成功");


                    FileInputStream ins = new FileInputStream(new File(path.toString()));
                    System.out.println(ins);
                    XSSFWorkbook workbook = new XSSFWorkbook(ins);

                    List<Position> plist = new ArrayList<>();
                    for (int i=10;i<workbook.getNumberOfSheets();i++){

//                    for (int i=10;i<11;i++){
                        Sheet sheet = workbook.getSheetAt(i);
                        System.out.println("报错了没4？");
                        int lastRowIndex = sheet.getLastRowNum();
                        System.out.println("共有" + lastRowIndex + "条记录");

                        Row rowOne = sheet.getRow(0);
                        //获取职位名
                        String positionName = rowOne.getCell(0).getStringCellValue();
                        Row rowTwo = sheet.getRow(1);

                        for (int t = 1;t<8;t++){
                            //获取等级
                            String grade = rowTwo.getCell(t).getStringCellValue();
                            ArrayList<Integer> list = new ArrayList<>();
                            for (int j = 2; j <= lastRowIndex; j++) {
                                Row row = sheet.getRow(j);
                                if (row == null) {
                                    return R.ok(90002,"第"+(j+1)+"数据异常");
                                }

                                Integer value = (int)row.getCell(t).getNumericCellValue();
                                list.add(value);
                            }
                            Position position = new Position();
                            position.setPositionName(positionName);
                            position.setGrade(grade);
                            position.setTotalSalary(list.get(0));
                            position.setYearlySalary(list.get(1));
                            position.setWelfare(list.get(2));
                            position.setHousingAllowance(list.get(3));
                            position.setVehicleAllowance(list.get(4));
                            position.setTravelAllowance(list.get(5));
                            position.setClothingAllowance(list.get(6));
                            position.setOtherAllowance(list.get(7));

                            plist.add(position);
                        }
                    }

                    System.out.println("报错了没1？");
                    File deleteFile = new File(path.toString());
                    deleteFile.delete();
                    return R.ok(90001,"文件上传成功").put("plist",plist);
                } else {
                    dic.mkdir();
                    byte[] bytes = file.getBytes();
                    Path path = Paths.get(uploadDir + file.getOriginalFilename());
                    Files.write(path, bytes);
                    return R.ok(90001,"文件上传成功");
                }

            } catch (IOException e) {
                e.printStackTrace();
                return R.error(90003,"文件上传失败");
            }
        }
    }
}
